package zozopo.warehouse.report;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;

import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class SalesReportAction extends ActionSupport implements Action,SessionAware{
	private String limitedCode = "REPORT.SALES";
	//传入参数
	private String searchStr = "";
	private int day = -7;
	private String order = "sumprofit";
	private String sort = "DESC";
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	//传出参数
	private List<ReceiptTB> receiptList;
	private Map<Integer,GoodsTB> goodsInfoMap;
	 
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
	
		
		ReceiptOS receiptOS = new ReceiptOS();
		GoodsOS goodsOS = new GoodsOS(receiptOS.getConn());
		if(searchStr.equals("")){
			receiptList = receiptOS.selectSalesReports(beginPageNum, pageSize,day,order,sort);
			 
			rowNum = receiptOS.getRowNumForSalesReports(day);
		}else{
			receiptList = receiptOS.selectSalesReportsBySearchStr(searchStr, beginPageNum, pageSize,day,order,sort);
			
			rowNum = receiptOS.getRowNumForSearchStrForSalesReports(searchStr,day);
		}
		
		/* in Str 组成*/
		String inStr = "(";
		boolean isFirst = true;
	
		for(ReceiptTB receipt:receiptList){
			if(isFirst){
				inStr += receipt.getGoodsid_receipt();
				isFirst = false;
			}else{
				inStr += "," + receipt.getGoodsid_receipt();
			}
		}
		inStr +=")";
		/* in Str 组成  END*/
		
		goodsInfoMap = goodsOS.getMapByInstr(inStr);
		
		receiptOS.releaseConnection(); 
		 
		return SUCCESS;
		
	}
	

	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有查看销售报表权限.");
			return;
		}
		
	}
		
	
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		//this.searchCode = searchStr;
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		//this.searchStr = searchStr;
	}
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	public List<ReceiptTB> getReceiptList() {
		return receiptList;
	}
	public void setReceiptList(List<ReceiptTB> receiptList) {
		this.receiptList = receiptList;
	}

	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getLimitedCode() {
		return limitedCode;
	}
	public void setLimitedCode(String limitedCode) {
		this.limitedCode = limitedCode;
	}
	public Map<Integer, GoodsTB> getGoodsInfoMap() {
		return goodsInfoMap;
	}
	public void setGoodsInfoMap(Map<Integer, GoodsTB> goodsInfoMap) {
		this.goodsInfoMap = goodsInfoMap;
	}

}
